package Frame.librarianFrame;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import controller.Controller;
import table.Book;
import javax.swing.ImageIcon;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableCellRenderer;

import Frame.common.Tab;
import Frame.common.TableBackGround;

import java.awt.SystemColor;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;

public class BookManageFrame extends JFrame {
	private JTextField tf1;
	private JTextField tf2;
	private JTextField tf3;
	private JTable bookTable;
	private JTextField tf4;
	private JTextField tf5;
	private JTextField tf6;
	private JTextField tf7;
	private JTextField tf8;
	private JTextField tf10;
	private JTextField tf9;
	private Controller controller;
	private JScrollPane tablePanel;
	private JComboBox<String> comboBox;
	private JPanel panel2;
	private JPanel panel3;
	private JLabel lb13;
	private JLabel lb12;
	private JPanel jpanel;

	public BookManageFrame(Controller controller) {
		this.controller = controller;
		//controller.setBookManageFrame(this);
		jpanel = new JPanel();
		jpanel.setBackground(Color.WHITE);		 
		jpanel.setLayout(null); 
		jpanel.add(getTablePanel(controller.getLibrarianService().find()));
		jpanel.add(panel2());
		jpanel.add(panel3());
		jpanel.setSize(1920,994);
		setContentPane(jpanel);
	}

	public JPanel panel3() {
		panel3 = new JPanel();
		panel3.setBackground(Color.WHITE);
		panel3.setBounds(0, 0, 1920, 127);
		panel3.setLayout(null);

		JLabel lb1 = new JLabel("索书号:");
		lb1.setBounds(153, 54, 64, 27);
		lb1.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel3.add(lb1);

		JLabel lb2 = new JLabel("书名:");
		lb2.setBounds(581, 54, 44, 27);
		lb2.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel3.add(lb2);

		JLabel lb3 = new JLabel("作者:");
		lb3.setBounds(971, 54, 44, 27);
		lb3.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel3.add(lb3);
 
		tf1 = new JTextField();
		tf1.setBounds(231, 52, 171, 31);
		tf1.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel3.add(tf1);
		tf1.setColumns(10);

		tf2 = new JTextField();
		tf2.setBounds(639, 52, 165, 31);
		tf2.setColumns(10);
		tf2.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel3.add(tf2);

		tf3 = new JTextField();
		tf3.setBounds(1029, 52, 165, 31);
		tf3.setColumns(10);
		tf3.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel3.add(tf3);

		JButton bt8 = new JButton("查询");
		bt8.setBounds(1341, 50, 119, 35);
		bt8.setIcon(new ImageIcon(BookManageFrame.class.getResource("/logo/搜索.png")));
		bt8.setFont(new Font("华文楷体", Font.PLAIN, 20));
		bt8.addActionListener(controller);
		bt8.setActionCommand("findBook");
		bt8.setContentAreaFilled(false);// 将按钮设置为透明
		panel3.add(bt8);
//		contentPane.add(panel3);
		JButton bt9 = new JButton("返回");
		bt9.setIcon(new ImageIcon(BookManageFrame.class.getResource("/logo/搜索.png")));
		bt9.setFont(new Font("华文楷体", Font.PLAIN, 20));
		bt9.setContentAreaFilled(false);
		bt9.addActionListener(controller);
		bt9.setActionCommand("findAllBook");
		bt9.setBounds(1586, 50, 119, 35);
		panel3.add(bt9);
		return panel3;
	}

	public JPanel panel2() {
		panel2 = new JPanel();
		panel2.setBackground(Color.WHITE);
		panel2.setBounds(0, 574, 1920, 333);
		panel2.setLayout(null);
		JLabel lb4 = new JLabel("书名:");
		lb4.setBounds(632, 43, 44, 27);
		lb4.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(lb4);

		JLabel lb6 = new JLabel("作者:");
		lb6.setBounds(632, 136, 44, 27);
		lb6.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(lb6);

		JLabel lb8 = new JLabel("数量:");
		lb8.setBounds(1050, 136, 44, 27);
		lb8.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(lb8);

		JLabel lb10 = new JLabel("价格:");
		lb10.setBounds(1050, 43, 44, 27);
		lb10.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(lb10);

		JLabel lb5 = new JLabel("索书号:");
		lb5.setBounds(215, 136, 64, 27);
		lb5.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(lb5);

		JLabel lb7 = new JLabel("ISBN:");
		lb7.setBounds(228, 43, 57, 27);
		lb7.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(lb7);

		JLabel lb9 = new JLabel("出版社:");
		lb9.setBounds(1455, 43, 64, 27);
		lb9.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(lb9);

//		JLabel lb11 = new JLabel("状态:");
//		lb11.setBounds(1050, 43, 44, 27);
//		lb11.setFont(new Font("华文楷体", Font.PLAIN, 20));
//		panel2.add(lb11);

		tf4 = new JTextField();
		tf4.setBounds(288, 43, 170, 31);
		tf4.setColumns(10);
		tf4.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(tf4);

		tf5 = new JTextField();
		tf5.setBounds(288, 136, 300, 31);
		tf5.setColumns(10);
		tf5.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(tf5);

		tf6 = new JTextField();
		tf6.setBounds(687, 43, 190, 31);
		tf6.setColumns(10);
		tf6.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(tf6);

		tf7 = new JTextField();
		tf7.setBounds(687, 136, 147, 31);
		tf7.setColumns(10);
		tf7.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(tf7);

		tf8 = new JTextField();
		tf8.setBounds(1108, 136, 138, 31);
		tf8.setColumns(10);
		tf8.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(tf8);

		tf10 = new JTextField();
		tf10.setBounds(1108, 41, 138, 31);
		tf10.setColumns(10);
		tf10.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(tf10);

		tf9 = new JTextField();
		tf9.setBounds(1533, 43, 201, 31);
		tf9.setColumns(10);
		tf9.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(tf9);

		JButton bt9 = new JButton("添加");
		bt9.setBounds(434, 223, 119, 35);
		bt9.setIcon(new ImageIcon(BookManageFrame.class.getResource("/logo/添加.png")));
		bt9.setFont(new Font("华文楷体", Font.PLAIN, 20));
		bt9.addActionListener(controller);
		bt9.setActionCommand("saveBook");
		panel2.add(bt9);

		JButton bt10 = new JButton("修改");
		bt10.setBounds(896, 223, 119, 35);
		bt10.setIcon(new ImageIcon(BookManageFrame.class.getResource("/logo/编辑.png")));
		bt10.setFont(new Font("华文楷体", Font.PLAIN, 20));
		bt10.addActionListener(controller);
		bt10.setActionCommand("updateBook");
		panel2.add(bt10);

		JButton bt11 = new JButton("删除");
		bt11.setBounds(1355, 223, 119, 35);
		bt11.setIcon(new ImageIcon(BookManageFrame.class.getResource("/logo/删除.png")));
		bt11.setFont(new Font("华文楷体", Font.PLAIN, 20));
		bt11.addActionListener(controller);
		bt11.setActionCommand("deleteBook");
		panel2.add(bt11);

//		comboBox = new JComboBox<String>();
//		comboBox.setBounds(1108, 40, 138, 33);
//		comboBox.setModel(new DefaultComboBoxModel(new String[] { "借出", "在馆" }));
//		comboBox.setFont(new Font("华文楷体", Font.PLAIN, 20));
//		panel2.add(comboBox);

		lb12 = new JLabel("请输入以“数字”为格式的数量");
		lb12.setBounds(1108, 170, 280, 27);
		lb12.setForeground(Color.RED);
		lb12.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(lb12);
		lb12.setVisible(false);

		lb13 = new JLabel("请输入以“数字”为格式的价格");
		lb13.setBounds(1108, 78, 280, 27);
		lb13.setForeground(Color.RED);
		lb13.setFont(new Font("华文楷体", Font.PLAIN, 20));
		panel2.add(lb13);
		lb13.setVisible(false);
//		contentPane.add(panel2);
		return panel2;
	}

	/**
	 * 滚动表格
	 * 
	 * @param bookList
	 * @return
	 */
	public JScrollPane getTablePanel(List<Book> bookList) {
		Object dataName[] = { "书名", "作者", "出版社", "索书号", "数量", "价格", "ISBN" };
		Object data[][] = new Object[bookList.size()][7];
		Iterator<Book> iter = bookList.iterator();
		int i = 0; 
		while (iter.hasNext()) {
			Book bk = iter.next();
			data[i][0] = bk.getBookName();
			data[i][1] = bk.getBookWriter();
			data[i][2] = bk.getBookPublishingHouse();
			data[i][3] = bk.getBookCallNumber();
			data[i][4] = bk.getBookNumber();
			data[i][5] = bk.getBookPrice(); 
			data[i][6] = bk.getBookIsbn();
			i++;
		}
		bookTable = new JTable(data, dataName) {
			@Override
		    public Component prepareRenderer(TableCellRenderer renderer, int row, int column){//设置鼠标光标划过时的颜色改变
	        Component comp = super.prepareRenderer(renderer,row,column); 
		        Point p = getMousePosition();
		    	
		        if(p!=null){
		            int rowUnderMouse = rowAtPoint(p);
		            if(rowUnderMouse == row){
		                  comp.setBackground(new Color(223,240,216));
		           }
//		            else{
//		                  comp.setBackground(DefaultLookup.getColor(this,ui,"Table.alternateRowColor"));
//		           }
		        }		        
		        return comp;
		    }
			private static final long serialVersionUID = 1L;

			public boolean isCellEditable(int row, int column) {
				return false;
			};// 将表设置成无法修改
		};
		bookTable.setSelectionBackground(new Color(223,240,216));//设置选中的行的颜色
		TableBackGround.setColumnColor(bookTable);//设置表的交替行数的颜色 
		bookTable.addMouseMotionListener(controller.getMyTableMouseMotion());
		bookTable.getTableHeader().setFont(new Font("华文楷体", Font.PLAIN, 20)); // 表头字体
		bookTable.setFont(new Font("华文楷体", Font.PLAIN, 21)); // 表体字体
		bookTable.getTableHeader().setBackground(new Color(184,207,240));//设置表头的颜色
		bookTable.getTableHeader().setPreferredSize(new Dimension(15, 35)); // 表头高度 先后为宽 高
		DefaultTableCellRenderer cr = new DefaultTableCellRenderer();// 将表体的内容设置为居中显示
		bookTable.setDefaultRenderer(Object.class, cr);
		bookTable.setRowHeight(bookTable.getRowHeight() + 15); // 表体高度
		bookTable.setRowMargin(5);// 设置相邻行中的单元格之间的空白空间量
		bookTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); // 表格只能选择一行
		bookTable.addMouseListener(controller);
		bookTable.setName("getBookMessage");
		bookTable.repaint(); 
		tablePanel = new JScrollPane(bookTable);
		tablePanel.getViewport().setBackground(Color.WHITE);//将JScrollPane设置成白色
		tablePanel.setBounds(0, 128, 1920, 447);
		// contentPane.add(tablePanel);
		// bookTable.setLocation(0, 210);
		// bookTable.setSize(1935, 528);
		// bookTable.setVisible(false);
		return tablePanel;
	}

	public void showSearchResult(List<Book> result) {
		jpanel.remove(tablePanel);
		jpanel.add(getTablePanel(result));
		jpanel.validate();
		JOptionPane.showMessageDialog(this, "重新加载数据成功！");
	}

	public void refresh() {
		jpanel.remove(tablePanel);
		jpanel.add(getTablePanel(controller.getLibrarianService().find()));
		jpanel.validate();
		JOptionPane.showMessageDialog(this, "重新加载数据成功！");
	}

	public JTextField getTf1() {
		return tf1;
	}

	public void setTf1(JTextField tf1) {
		this.tf1 = tf1;
	}

	public JTextField getTf2() {
		return tf2;
	}

	public void setTf2(JTextField tf2) {
		this.tf2 = tf2;
	}

	public JTextField getTf3() {
		return tf3;
	}

	public void setTf3(JTextField tf3) {
		this.tf3 = tf3;
	}

	public JTextField getTf4() {
		return tf4;
	}

	public void setTf4(JTextField tf4) {
		this.tf4 = tf4;
	}

	public JTextField getTf5() {
		return tf5;
	}

	public void setTf5(JTextField tf5) {
		this.tf5 = tf5;
	}

	public JTextField getTf6() {
		return tf6;
	}

	public void setTf6(JTextField tf6) {
		this.tf6 = tf6;
	}

	public JTextField getTf7() {
		return tf7;
	}

	public void setTf7(JTextField tf7) {
		this.tf7 = tf7;
	}



	public JTextField getTf8() {
		return tf8;
	}

	public void setTf9(JTextField tf9) {
		this.tf8 = tf9;
	}

	public JTextField getTf9() {
		return tf9;
	}

	public void setTf10(JTextField tf10) {
		this.tf10 = tf10;
	}

	public JTextField getTf10() {
		return tf10;
	}

	public void setTf11(JTextField tf11) {
		this.tf9 = tf11;
	}

	public JComboBox<String> getComboBox() {
		return comboBox;
	}

	public void setComboBox(JComboBox<String> comboBox) {
		this.comboBox = comboBox;
	}

	public void setTablePanel(JScrollPane tablePanel) {
		this.tablePanel = tablePanel;
	}

	public JScrollPane getTablePanel() {
		return tablePanel;
	}

	public void setTf8(JTextField tf8) {
		this.tf8 = tf8;
	}

	public JLabel getLb13() {
		return lb13;
	}

	public void setLb13(JLabel lb13) {
		this.lb13 = lb13;
	}

	public JLabel getLb12() {
		return lb12;
	}

	public void setLb12(JLabel lb12) {
		this.lb12 = lb12;
	}

	public JPanel getPanel2() {
		return panel2;
	}

	public void setPanel2(JPanel panel2) {
		this.panel2 = panel2;
	}

	public JPanel getPanel3() {
		return panel3;
	}

	public void setPanel3(JPanel panel3) {
		this.panel3 = panel3;
	}

	public JTable getBookTable() {
		return bookTable;
	}

	public void setBookTable(JTable bookTable) {
		this.bookTable = bookTable;
	}

	public JPanel getJpanel() {
		return jpanel;
	}

	public void setJpanel(JPanel jpanel) {
		this.jpanel = jpanel;
	}
}
